﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace GenerateDictionary
{
    [Serializable]
    public class Configuration
    {
        public int NumberofThreadsSelected;
        public int MaxCodeSize; //codelength
        public string dirpath = "dir";
        public int MinCodeSize;
        public string Filepath; //file name for file combining.
        public List<string> ThreadTempFilepaths;
        public bool iscustomtempfile;
        public List<char> charactureset = new List<char>();
        public bool isStandardcharset;
        public bool[] StandardCharsetincludes;
        public bool cleanuptempfiles;

        public Configuration()
        {
            LoadDefaults();

        }

        private void LoadDefaults()
        {
            StandardCharsetincludes = new bool[3];
            StandardCharsetincludes[0] = true; //numbers
            StandardCharsetincludes[1] = true; //A-Z upper case
            StandardCharsetincludes[2] = true; //a-z lower case

            
            iscustomtempfile = false;
            ThreadTempFilepaths = new List<string>();
            NumberofThreadsSelected = 1;
            MaxCodeSize = 1;
            MinCodeSize = 1;
            Filepath = "";
            dirpath = "";
            isStandardcharset = true;
            charactureset.Clear();
            charactureset.AddRange(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
            cleanuptempfiles = true;
        }

        public string SettingsListforView()
        {
            string settings = "\r\nSETTINGS\r\n";

            settings += "1.) File full path (code gen final output file): " +"[" + (string.IsNullOrEmpty(Filepath)? "Code Gen File Path is not set":Filepath) + "]"+"\r\n\r\n";
            settings += "2.) Number of concurrent processing threads selected: " + "[" + NumberofThreadsSelected.ToString() + "]" + "\r\n\r\n";
            settings += "3.) "+(iscustomtempfile ? "Custom temporary file locations have been set."+"\r\n\r\n" : "Temporary file locations are set to be automaically generated."+"\r\n\r\n");
            if (isStandardcharset)
            {
                settings += "4.) Standard Characture sets selected:";
                settings += "\r\n\t" + charsetsstandardlist();


            }
            else
            {
                settings += "4.) Custom Characture sets selected:";
                settings += "\r\n\t" + charsetCustomlist();

            }

            settings += "5.) Code generation minimum length: ["+MinCodeSize.ToString() +"] maximum length: " + "[" + MaxCodeSize.ToString() + "]" + "\r\n\r\n";
            if (iscustomtempfile)
            {
                settings += "6.) " + (cleanuptempfiles ? "Remove temp files after processing selected." + "\r\n\r\n" : "Temporary files are set to be available post code generation." + "\r\n\r\n");
                settings += "7.) Temporary file paths set: ";
                settings += "\r\n\t" + tempfilepathsStringlist();
                settings += "\r\n";
            }
            

            return settings;
        }

        private string charsetCustomlist()
        {
            string temp = "";
            int Nlevery = 0;
            for (int x = 0; x < charactureset.Count; x++)
            {
                if (x == 0)
                {

                    temp += "(  " + Utility.Utility.getnicecharvalueonint((int)charactureset[x]) + " - ";
                }
                else
                {

                    if (Nlevery == 5)
                    {
                        if (x == charactureset.Count - 1)
                        {
                            temp += "  )\r\n\t(  " + Utility.Utility.getnicecharvalueonint((int)charactureset[x]);
                        }
                        else
                        {
                            temp += "  )\r\n\t(  " + Utility.Utility.getnicecharvalueonint((int)charactureset[x]) + " - ";
                        }
                        Nlevery = 0;

                    }
                    else if (Nlevery == 4)
                    {
                        temp += Utility.Utility.getnicecharvalueonint((int)charactureset[x]);

                    }
                    else
                    {
                        if (x == charactureset.Count - 1)
                        {
                            temp += Utility.Utility.getnicecharvalueonint((int)charactureset[x]);
                        }
                        else
                        {
                            temp += Utility.Utility.getnicecharvalueonint((int)charactureset[x]) + " - ";
                        }
                    }

                }

                if (x == charactureset.Count - 1)
                {
                    temp += "  )\r\n\r\n";
                }

                Nlevery++;
            }


            return temp;
        }

        private string charsetsstandardlist()
        {
            string temp = "";

            for (int x = 0; x < StandardCharsetincludes.Count(); x++)
            {
                if (StandardCharsetincludes[x])
                {
                    if (x == 0)
                    {
                        temp += "Numbers (0-9)";
                    }
                    else if(x==1)
                    {
                        temp += "\r\n\tUpper Case Charactures (A-Z)";
                    }
                    else if (x == 2)
                    {
                        temp += "\r\n\tLower Case Charactures (a-z)";
                    }
                    
                }
                if (x == 2)
                {
                    temp += "\r\n\r\n";
                }
            }
            return temp;
        }

        private string tempfilepathsStringlist()
        {
            string temp = "";
            if (ThreadTempFilepaths.Count() > 0)
            {
                for (int x = 0; x < ThreadTempFilepaths.Count(); x++)
                {
                    if (ThreadTempFilepaths[x] != null)
                    {
                        if (x == 0)
                        {
                            temp = ThreadTempFilepaths[x];
                        }
                        else
                        {
                            temp += "\r\n\t" + ThreadTempFilepaths[x];
                        }

                    }

                }

            }
            else
            {
                temp = "No temporary file paths set\r\n";

            }
            return temp;
        }

        public bool[] testConfigfilepaths(Configuration cfg)
        {

            var filevalid = new bool[cfg.ThreadTempFilepaths.Count + 1];
            var dirInf = new DirectoryInfo(cfg.Filepath);

            filevalid[0] = dirInf.Exists;

            for (int x = 0; x < cfg.ThreadTempFilepaths.Count; x++)
            {
                var Inf = new DirectoryInfo(cfg.ThreadTempFilepaths[x]);
                filevalid[x + 1] = Inf.Exists;
            }

            return filevalid;
        }

        public bool[] testConfigfilepaths()
        {

            var filevalid = new bool[ThreadTempFilepaths.Count + 1];
            var dirInf = new DirectoryInfo(System.IO.Path.GetDirectoryName(Filepath));

            filevalid[0] = dirInf.Exists;

            for (int x = 0; x < ThreadTempFilepaths.Count; x++)
            {
                var Inf = new DirectoryInfo(System.IO.Path.GetDirectoryName(ThreadTempFilepaths[x]));
                filevalid[x + 1] = Inf.Exists;
            }

            return filevalid;
        }
               
        }
    }

